from fastapi import APIRouter, HTTPException, Depends
from typing import List
from sqlalchemy.orm import Session
from schema import Farm_Admin_schema
from controller import Farm_Admin_controller
from database.db_init import get_db


router = APIRouter()


@router.post("/Farm_Admin", response_model=Farm_Admin_schema.Farm_admin, tags=["Farm_Admin"])
async def create_Farm_Admin(Farm_Admin: Farm_Admin_schema.Farm_admin, db: Session = Depends(get_db)):
    return Farm_Admin_controller.create_Farm_Admin(db, Farm_Admin)


@router.get("/Farm_Admin/{ID}", response_model=Farm_Admin_schema.Farm_admin, tags=["Farm_Admin"])
async def get_Farm_Admin(ID: int, db: Session = Depends(get_db)):
    db_Farm_Admin = Farm_Admin_controller.get_Farm_Admin(db, ID)
    if db_Farm_Admin is None:
        raise HTTPException(status_code=404, detail="Farm_Admin not found")
    return db_Farm_Admin


@router.get("/Farm_Admin", response_model=List[Farm_Admin_schema.Farm_admin], tags=["Farm_Admin"])
async def get_all_Farm_Admin(db: Session = Depends(get_db)):
    all_db_Farm_Admin = Farm_Admin_controller.get_all_Farm_Admin(db)
    if all_db_Farm_Admin is None:
        raise HTTPException(status_code=404, detail="0 Farm_Admin found, empty table")
    return all_db_Farm_Admin


@router.put("/Farm_Admin/{ID}", tags=["Farm_Admin"])
async def update_Farm_Admin(ID: int, field_name: str, field_value: str, db: Session = Depends(get_db)):
    try:
        db_Farm_Admin = Farm_Admin_controller.update_Farm_Admin(db, ID, field_name, field_value)
        if db_Farm_Admin is None:
            raise HTTPException(status_code=404, detail="Farm_Admin not found")
        return db_Farm_Admin
    except ValueError as error:
        raise HTTPException(status_code=400, detail=str(error))


@router.delete("/Farm_Admin/{ID}", tags=["Farm_Admin"])
async def delete_Farm_Admin(ID: int, db: Session = Depends(get_db)):
    is_deleted = Farm_Admin_controller.delete_Farm_Admin(db, ID)
    if is_deleted is None:
        raise HTTPException(status_code=404, detail=f'Sample {ID} does not exist in database')
    else:
        return {'message': f'Successfully delete sample {ID}'}
